
$(function() {
	$('#rate').val(exchange);	
	_rmb();
	
	$('#amount').change(function() {
		var s = $(this).val();
		if (s != null && s != '-')　{
			_rmb();
		}		
	});
	
	$('#rate').change(function() {
		var s = $(this).val();
		if (s != null && s != '-')　{
			_rmb();
		}		
	});
	
	$("input[id^='invoice']:visible").datepicker(_date1_option);
	$("input[id^='date']:visible").datepicker(_date1_option);
	
	$('#transaction').change(function() {		
		_transaction($(this).val());
	});
	
	$('#transaction').blur(function() {		
		_transaction($(this).val());
	});
	
	_transaction($('#transaction').val());
	
	$("input[id='kc_c']:visible").click(function() {
		var a = $("#amount").val();
		var b = $("input[id='kc']:visible").val();
		
		a = a.replace(/,/g, '');
		b = b.replace(/,/g, '');
		if ($(this).attr("checked")) {
			c = a - b;
		} else {
			c = fxadd(a, b);
		}
		c = fmoney(c, 2);		
		c = c.replace(/,/g, '');
		
		$("#amount").val(c);
		
		_rmb();
	});
	
	$('#transaction_t').change(function() {
		var s = $(this).val();
		if (s != null && s != '-')　{
			$('#transaction').val(s);
			
			_transaction(s);
		}
	});
	
	$('textarea').css("width", 400);
});

function _transaction(s) {
	var body = { };
	body.id = s;
	
	$.getJSON('finance.php?do=Transaction.ajax_id', body, function(json) {
		
		$("#date").val(json.date);
		$("#kc").val(json.kc);
		
		var a = $("#amount").val();
		if (a == null || a == '' || a == '0.00') {
			var b = $("#total").val();
			b  = fmoney(b, 2);
			b = b.replace(/,/g, '');
			
			$("#amount").val(b);
		}		
	});
}

function _rmb() {
	var a = $("#amount").val();
	var b = $('#rate').val();
	a = a.replace(/,/g, '');
	b = b.replace(/,/g, '');
	
	var rmb = fxmul(a, b);
	
	rmb  = fmoney(rmb, 2);
	rmb = rmb.replace(/,/g, '');
	
	$('#rmb').val(rmb);	
}
   